package com.tanhua.dubbo.api;

import com.tanhua.domain.mg.UserLocation;
import com.tanhua.domain.vo.UserLocationVo;
import com.tanhua.dubbo.mg.UserLocationApi;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Circle;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.Metrics;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import java.util.List;

@Service
public class UserLocationApiImpl implements UserLocationApi {

    //注入MonGoTemplate对象
    @Autowired
    private MongoTemplate mongoTemplate;
    //订阅UserInfoApi
    @Reference
    private UserInfoApi userInfoApi;

    /**
     * 上报地理信息
     */
    @Override
    public void saveLocation(Double latitude, Double longitude, String addrStr, Long currentUserId,String gender) {
            //设置一个统一的时间戳
        long timeMillis = System.currentTimeMillis();
        //设置查询条件 根据当前用户id查询位置表
        Query query = new Query();
        query.addCriteria(Criteria.where("userId").is(currentUserId));
            //查询
        UserLocation userLocation = mongoTemplate.findOne(query, UserLocation.class);
            //判断userLocation是否为null
        if(userLocation==null){
            //执行插入语句
            userLocation = new UserLocation();
            userLocation.setId(ObjectId.get());//设置id
            userLocation.setUserId(currentUserId);//当前用户
            if(latitude>longitude){//防止前端传输异常
                userLocation.setLocation(new GeoJsonPoint(latitude,longitude));//设置经纬度
            }
            else {
                userLocation.setLocation(new GeoJsonPoint(longitude,latitude));//设置经纬度
            }
            userLocation.setAddress(addrStr);//设置地址
            userLocation.setCreated(timeMillis);//设置创建时间
            userLocation.setUpdated(timeMillis);//更新时间
            userLocation.setLastUpdated(timeMillis);//最近更新时间
            //设置性别属性
            userLocation.setGender(gender);
            //执行
            mongoTemplate.save(userLocation,"user_location");
        }
        else {
            //执行更新
            Update update = new Update();
            update.set("address",addrStr);//地理位置
            update.set("updated",timeMillis);//更新时间
            update.set("lastUpdated",timeMillis);//最近更新时间
            update.set("gender",gender);//设置性别属性
            if(latitude>longitude){//防止前端传输异常
                update.set("location",new GeoJsonPoint(latitude,longitude));
            }
            else {
                update.set("location",new GeoJsonPoint(longitude,latitude));
            }
            mongoTemplate.updateFirst(query,update,UserLocation.class);
        }


    }
    /**
     * 搜附近
     */
    @Override
    public List<UserLocationVo> searchNearby(Long currentUserId, Long distance, String gender) {
        Query query = new Query();
        //根据当前id查询地理位置信息表
        query.addCriteria(Criteria.where("userId").is(currentUserId));
        UserLocation userLocation = mongoTemplate.findOne(query, UserLocation.class);
        //获取地理位置信息
        GeoJsonPoint location = userLocation.getLocation();
        //c.创建条件
        Query nearQuery = new Query();
        //d.根据圆心距离创建一个圆
        Circle circle = new Circle(location,
                new Distance(distance / 1000, Metrics.KILOMETERS));
        //e.设置查询条件
        nearQuery.addCriteria(Criteria.where("gender").is(gender)
                .and("location").withinSphere(circle));
        //f.调用MongoTemplate对象 查询 返回List<Userlocation>
        List<UserLocation> userLocationList = mongoTemplate.find(nearQuery, UserLocation.class);
        //g.调用UserLocaitonVo对象的format方法 转换集合类型
        List<UserLocationVo> userLocationVoList = UserLocationVo.formatToList(userLocationList);
        //h.返回数据
        return userLocationVoList;
    }
}
